import service from '../../controller/service.js';

Page({
  data: {
    cateItems: [
      {
        cate_id: 1,
        cate_name: "男包",
        ishaveChild: true,
        children:
        [
          {
            child_id: 1,
            name: '耐克',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117972563.jpg"
          },
          {
            child_id: 2,
            name: '安踏',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161207/148110444480.jpg"
          },
          {
            child_id: 3,
            name: '阿迪达斯',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117973270.jpg"
          },
          {
            child_id: 4,
            name: '新百伦',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117981591.jpg"
          }
        ]
      },
      {
        cate_id: 2,
        cate_name: "女包",
        ishaveChild: true,
        children:
        [
          {
            child_id: 1,
            name: '李宁',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/14815381301.jpg"
          },
          {
            child_id: 2,
            name: '格力',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/14815381411.jpg"
          },
          {
            child_id: 3,
            name: '美的',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153815181.jpg"
          },
          {
            child_id: 4,
            name: '腮红',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153815759.jpg"
          },
          {
            child_id: 5,
            name: '粉饼',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153816983.jpg"
          },
          {
            child_id: 6,
            name: '粉底',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153817721.jpg"
          },
          {
            child_id: 7,
            name: '蜜粉/散粉',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153819354.jpg"
          },
          {
            child_id: 8,
            name: '隔离霜',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161215/148179053369.jpg"
          }
        ]
      },
      {
        cate_id: 3,
        cate_name: "男装",
        ishaveChild: true,
        children:
        [
          {
            child_id: 1,
            name: '淡香水EDT',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161213/14815978910.jpg"
          },
          {
            child_id: 2,
            name: '浓香水EDP',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161213/148159789883.jpg"
          },
          {
            child_id: 3,
            name: '香体走珠',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161213/14815979307.jpg"
          },
          {
            child_id: 4,
            name: '古龙香水男士的最爱',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161213/148159765589.jpg"
          }
        ]
      },
      {
        cate_id: 4,
        cate_name: "女装",
        ishaveChild: false,
        children: []
      },
      {
        cate_id: 5,
        cate_name: "男鞋",
        ishaveChild: false,
        children: []
      },
      {
        cate_id: 6,
        cate_name: "女鞋",
        ishaveChild: false,
        children: []
      },
    ],
    curNav: 1,
    curIndex: 0,

    firstcd:[],
    secondcd:[],
    ishaveSecond:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    //获取一级菜单数据
    service.getClassifyone(
      (res) => {
        that.setData({
          firstcd:res.data
        });
        console.log(that.data.firstcd)
      },
      (err) => {
        console.log(err)
      }
    );
    this.getSecondcd();
  },
    
  //获取二级菜单数据
  getSecondcd:function(){
    var that=this
    service.getClassifyTwo(that.data.curNav,
      (res)=>{
        that.getSecondcdSuccess(res)
      },
      (err) => {
        console.log(err)
      }
    )
  },
  
  //获取二级菜单成功回调函数
  getSecondcdSuccess:function(res){
    var that=this;
    that.setData({
      secondcd: res.data
    });
    console.log(that.data.secondcd)
    //判断有没有数据，有ishaveSecond设为true
    if (that.data.secondcd != "") {
      that.setData({
        ishaveSecond: true
      });
    } else {
      that.setData({
        ishaveSecond: false
      });
    }
  },
  //左侧item点击事件处理函数 
  switchRightTab: function (e) {
    // 获取item项的id，和数组的下标值 
    var that=this;
    let id = e.target.dataset.id,
      index = parseInt(e.target.dataset.index);
    // 把点击到的某一项，设为当前index 
    that.setData({
      curNav: id,
      curIndex: index
    });
    this.getSecondcd();
  }
})